import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/pages/match/modify_match/modify_match_controller.dart';
import 'package:flutter_changdian_football/utils/custom_logger.dart';
import 'package:flutter_changdian_football/utils/str_utils.dart';
import 'package:flutter_changdian_football/widget/custom_button.dart';
import 'package:flutter_changdian_football/widget/dialog/input_dialog.dart';
import 'package:flutter_changdian_football/widget/dialog/prompt_dialog.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:flutter_changdian_football/widget/sheet/param_picker.dart';
import 'package:flutter_changdian_football/widget/sheet/select_date_time_picker.dart';
import 'package:flutter_changdian_football/widget/switch/flutter_switch.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:get/get.dart';

/// 修改比赛
class ModifyMatchPage extends StatelessWidget {
  const ModifyMatchPage({super.key, required this.matchId, required this.regionCode});

  final String matchId;

  final String regionCode;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ModifyMatchController>(
      init: ModifyMatchController(matchId: matchId, regionCode: regionCode),
      builder: (modifyMatchController) {
        return Scaffold(
          backgroundColor: const Color(0xffF8FAF9 ),
          appBar: AppBar(
            centerTitle: true,
            elevation: 0,
            title: Text(
              '修改比赛信息',
              style: TextStyle(fontSize: 16.sp, color: const Color(0xff333333)),
            ),
            iconTheme: const IconThemeData(
              color: Color(0xff333333)
            ),
            backgroundColor: const Color(0xffF8FAF9 ),
          ),
          bottomNavigationBar: Container(
            // height: 66.w,
            padding: EdgeInsets.symmetric(horizontal: 17.w, vertical: 8.w),
            width: double.infinity,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                CustomButton(
                  text: '修改比赛',
                  onTap: () {
                    PromptDialog.show(
                      Get.context!,
                      title: '修改比赛信息确认',
                      desc: '修改比赛信息将退还所有已报名球员的报名费，您确认执行此操作吗',
                      cancelTap: () {
                        Get.back();
                      },
                      confirmTap: () {
                        Get.back();
                        modifyMatchController.modifyMatch();
                      }
                    );
                  },
                ),

                SizedBox(height: 10.w,),


                CustomButton(
                  text: '取消比赛',
                  onTap: () {
                    PromptDialog.show(
                      Get.context!,
                      title: '取消比赛确认',
                      desc: '取消将退还所有已报名球员的报名费并取消此次比赛，您确认执行此操作吗',
                      cancelTap: () {
                        Get.back();
                      },
                      confirmTap: () {
                        Get.back();
                        modifyMatchController.cancelMatch();
                      }
                    );
                  },
                )
              ],
            ),
          ),
          body: SizedBox(
            child: Column(
              children: [
                Expanded(
                  child: SingleChildScrollView(
                    padding: EdgeInsets.only(left: 17.w, right: 17.w, bottom: 30.w),
                    physics: const BouncingScrollPhysics(),
                    child: Column(
                      children: [
                        SizedBox(height: 10.w,),

                        Container(
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(12.r)
                          ),
                          child: Column(
                            children: [
                              _leftAndRightWidget(leftText: '场地', rightText: modifyMatchController.selectCourtIndex == -1 ? '请选择场地' : 
                                modifyMatchController.courtStrList[modifyMatchController.selectCourtIndex], onTap: () {
                                ParamPicker.show(
                                  '场地', 
                                  context, 
                                  modifyMatchController.courtStrList,
                                  selecteds: modifyMatchController.selectCourtIndex,
                                  onConfirm: (v) {
                                    modifyMatchController.changeSelectCourtIndex(v);
                                  }
                                );
                              }),
                              
                              Container(width: double.infinity, height: 0.5.w, color: const Color(0xffF8FAF9), margin: EdgeInsets.symmetric(horizontal: 12.w)),

                              _leftAndRightWidget(leftText: '场地费', rightText: modifyMatchController.selectSiteFeeIndex == -1 ? '请选择场地费' : '${modifyMatchController.siteFeeList[modifyMatchController.selectSiteFeeIndex]}元', onTap: () {
                                List<Map<String, List<String>>> selectHeight = [];
                                for (int i = 0; i < modifyMatchController.siteFeeList.length; i++) {
                                  selectHeight.add({
                                    '${modifyMatchController.siteFeeList[i]}' : ['元']
                                  });
                                }

                                ParamPicker.showDw(
                                  '场地费', 
                                  context, 
                                  selectHeight,
                                  selecteds: modifyMatchController.selectSiteFeeIndex == -1 ? 0 : modifyMatchController.selectSiteFeeIndex,
                                  onConfirm: (v) {
                                    modifyMatchController.changeSelectSiteFeeIndex(v);
                                  }
                                );


                                // ParamPicker.show(
                                //   '场地费', 
                                //   context, 
                                //   modifyMatchController.siteFeeList,
                                //   selecteds: modifyMatchController.selectSiteFeeIndex,
                                //   onConfirm: (v) {
                                //     modifyMatchController.changeSelectSiteFeeIndex(v);
                                //   }
                                // );
                              }),

                              Container(width: double.infinity, height: 0.5.w, color: const Color(0xffF8FAF9), margin: EdgeInsets.symmetric(horizontal: 12.w)),

                              _leftAndRightWidget(leftText: '人数上限', rightText: modifyMatchController.selectMaxNumOfPeopleIndex == -1 ? '请选择人数上限' : '${modifyMatchController.maxNumOfPeopleList[modifyMatchController.selectMaxNumOfPeopleIndex]}人', onTap: () {
                                // ParamPicker.show(
                                //   '人数上限', 
                                //   context, 
                                //   modifyMatchController.maxNumOfPeopleList,
                                //   selecteds: modifyMatchController.selectMaxNumOfPeopleIndex,
                                //   onConfirm: (v) {
                                //     modifyMatchController.changeSelectMaxNumOfPeopleIndex(v);
                                //   }
                                // );

                                List<Map<String, List<String>>> selectHeight = [];
                                for (int i = 0; i < modifyMatchController.maxNumOfPeopleList.length; i++) {
                                  selectHeight.add({
                                    '${modifyMatchController.maxNumOfPeopleList[i]}' : ['人']
                                  });
                                }


                                ParamPicker.showDw(
                                  '人数上限', 
                                  context, 
                                  selectHeight,
                                  selecteds: modifyMatchController.selectMaxNumOfPeopleIndex == -1 ? 0 : modifyMatchController.selectMaxNumOfPeopleIndex,
                                  onConfirm: (v) {
                                    modifyMatchController.changeSelectMaxNumOfPeopleIndex(v);
                                  }
                                );
                              }),
                            ],
                          ),
                        ),


                        SizedBox(height: 10.w,),

                        Container(
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(12.r)
                          ),
                          child: Column(
                            children: [
                              _leftAndRightWidget(leftText: '对手', rightText: modifyMatchController.opponent ?? '请输入对手名称', onTap: () {
                                modifyMatchController.opponentController.text = modifyMatchController.opponent ?? '';
                                InputDialog.show(
                                  Get.context!,
                                  title: "对手",
                                  hintText: '请输入对手名称',
                                  cancelTap: () {
                                    Get.back();
                                  },
                                  confirmTap: () {
                                    Get.back();
                                    modifyMatchController.changeOpponent();
                                  },
                                  controller: modifyMatchController.opponentController
                                );
                              }),
                              
                              Container(width: double.infinity, height: 0.5.w, color: const Color(0xffF8FAF9), margin: EdgeInsets.symmetric(horizontal: 12.w)),

                              _leftAndRightWidget(leftText: '球衣颜色', rightText: modifyMatchController.selectJerseyColorIndex == -1 ? '' : modifyMatchController.jerseyColorList[modifyMatchController.selectJerseyColorIndex], onTap: () {
                                ParamPicker.show(
                                  '球衣颜色', 
                                  context, 
                                  modifyMatchController.jerseyColorList,
                                  selecteds: modifyMatchController.selectJerseyColorIndex,
                                  onConfirm: (v) {
                                    modifyMatchController.chaneJerseyColor(v);
                                  }
                                );
                              }),

                            ],
                          ),
                        ),



                        SizedBox(height: 10.w,),

                        Container(
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(12.r)
                          ),
                          child: Column(
                            children: [
                              _leftAndRightWidget(leftText: '开始时间', rightText: modifyMatchController.beginDate == null ? '请选择开始时间' : StrUtils.formatYNDHM(modifyMatchController.beginDate!), onTap: () {
                                SelectDateTimePicker.show(
                                  '开始时间', 
                                  context, 
                                  modifyMatchController.beginDate == null ? DateTime.now().toString() : modifyMatchController.beginDate.toString(),
                                  type: PickerDateTimeType.kYMDHM,
                                  onConfirm: (v) {
                                    modifyMatchController.changeBeginDate(v.toString());
                                  }
                                );
                              }),
                              
                              Container(width: double.infinity, height: 0.5.w, color: const Color(0xffF8FAF9), margin: EdgeInsets.symmetric(horizontal: 12.w)),

                              _leftAndRightWidget(leftText: '结束时间', rightText: modifyMatchController.endDate == null ? '请选择结束时间' :  StrUtils.formatYNDHM(modifyMatchController.endDate!), onTap: () {
                                SelectDateTimePicker.show(
                                  '结束时间', 
                                  context, 
                                  modifyMatchController.endDate == null ? DateTime.now().toString() : modifyMatchController.endDate.toString(),
                                  type: PickerDateTimeType.kYMDHM,
                                  onConfirm: (v) {
                                    modifyMatchController.changeEndDate(v.toString());
                                  }
                                );
                              }),

                            ],
                          ),
                        ),



                        SizedBox(height: 10.w,),

                        Container(
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(12.r)
                          ),
                          child: Column(
                            children: [
                              _leftAndRightWidget(leftText: '赛制', rightText: modifyMatchController.selectSystemPlayIndex == -1 ? '请选择比赛赛制' : modifyMatchController.systemPlayStrList[modifyMatchController.selectSystemPlayIndex], onTap: () {
                                ParamPicker.show(
                                  '赛制', 
                                  context, 
                                  modifyMatchController.systemPlayStrList,
                                  selecteds: modifyMatchController.selectSystemPlayIndex,
                                  onConfirm: (v) {
                                    modifyMatchController.changeSelectSystemPlayIndex(v);
                                  }
                                );
                              }),
                              
                              Container(width: double.infinity, height: 0.5.w, color: const Color(0xffF8FAF9), margin: EdgeInsets.symmetric(horizontal: 12.w)),

                              _leftAndRightWidget(
                                leftText: '是否放水惩罚',
                                isRight: false,
                                onTap: () {
                                  modifyMatchController.changePunishment();
                                },
                                rightChild: FlutterSwitch(
                                  value: modifyMatchController.isPunishment, 
                                  width: 47.w,
                                  height: 29.w,
                                  toggleSize: 24.w,
                                  padding: 2.w,
                                  inactiveColor: const Color(0xffe1e1e1),
                                  toggleColor: Colors.white,
                                  activeColor: const Color(0xff499360),
                                  onToggle: (v) {
                                    modifyMatchController.changePunishment();
                                  }
                                )
                              ),

                            ],
                          ),
                        ),



                        SizedBox(height: 10.w,),


                        Container(
                          width: double.infinity,
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(12.r),
                          ),
                          height: 140.w,
                          child: Column(
                            children: [
                              Expanded(
                                child: TextField(
                                  keyboardType: TextInputType.text,
                                  maxLines: 10,
                                  controller: modifyMatchController.remarkController,
                                  decoration: InputDecoration(
                                    contentPadding: EdgeInsets.symmetric(vertical: 14.w, horizontal: 12.w),
                                    border: const OutlineInputBorder(
                                      borderSide: BorderSide.none,
                                    ),
                                    hintStyle: TextStyle(fontSize: 15.sp, color: const Color(0xff9E9E9E)),
                                    hintText: '比赛备注',
                                    counterText: '',
                                  ),
                                  style: TextStyle(fontSize: 15.sp, color: const Color(0xff9E9E9E)),
                                  onChanged: (v) {
                                  },
                                ),
                              )
                            ],
                          ),
                        ),

                        SizedBox(height: 10.w,),

                        SizedBox(
                          child: Text('*开启放水惩罚后，报名球员在比赛开始当天报水不退还报名场地费', style: TextStyle(fontSize: 12.sp, color: const Color(0xffcccccc)),),
                        )
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      }
    );
  }



  _leftAndRightWidget({
    String leftText = '',
    String rightText = '',
    bool isRight = true,
    Widget? rightChild,
    Function? onTap
  }) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        onTap?.call();
      },
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 14.w, horizontal: 12.w),
        child: Row(
          children: [
            SizedBox(
              child: Text(leftText, style: TextStyle(fontSize: 15.sp, color: const Color(0xff323232)),),
            ),
    
            // const Spacer(),

            SizedBox(width: 20.w,),
    
            rightChild ?? Expanded(
              child: Text(rightText, style: TextStyle(fontSize: 14.sp, color: const Color(0xff999999), height: 1.5), textAlign: TextAlign.end, maxLines: 2, overflow: TextOverflow.ellipsis,),
            ),
    
            if (isRight) ...[
              SizedBox(width: 8.w,),
    
              Container(
                margin: EdgeInsets.only(top: 1.w),
                width: 10.w,
                height: 10.w,
                child: Image.asset('assets/images/icon_arrow_grey_right.png'),
              )
            ],
          ],
        ),
      ),
    );
  }


}